from sklearn.externals import joblib
import os 
import pandas as pd
import numpy as np
from datetime import datetime
  
def file_m(file_dir):  
    L=[]  
    for root, dirs, files in os.walk(file_dir): 
        for file in files: 
            if os.path.splitext(file)[1] == '.m': 
                L.append(file) 
    return L
def file_txt(file_dir):  
    L=[]  
    for root, dirs, files in os.walk(file_dir): 
        for file in files: 
            if os.path.splitext(file)[1] == '.txt': 
                L.append(file) 
    return L

def car2price(mileage, time, brand):
    result = 0
    if brand+'.txt' in file_txt('./model'):
        clf = joblib.load("./model/"+brand+'.m')

        with open("./model/"+brand+'.txt', 'r') as f1:
            list1 = f1.readline()
        time_subtract = (datetime.now() - datetime.strptime(time,"%Y-%m")).days/365
        temp = pd.DataFrame([mileage,time_subtract,float(list1)]).transpose()
        temp.columns = ['mileage', 'time_subtract', 'price_y']
        result = np.exp(clf.predict(temp))-1
    else:
        result = None
    return result
    